package com.mall507.fivepeaks.activity.find;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.hzh.frame.core.ImageFrame.BaseImage;
import com.hzh.frame.ui.fragment.AbsRecyclerViewFM;
import com.hzh.frame.util.AndroidUtil;
import com.hzh.frame.widget.rxbus.MsgEvent;
import com.hzh.frame.widget.rxbus.RxBus;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.mall507.comn.model.LostFound;
import com.mall507.comn.model.Original;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.util.Util;
import com.mall507.fivepeaks.widget.imageview.CircleImageView;
import com.mall507.fivepeaks.widget.imageview.ExpandImageView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Predicate;

/**
 * 失物招领
 */
public class FindLostFoundRFM extends AbsRecyclerViewFM<LostFound> {
    public static final String TAG="FindLostFoundRFM";
    
    public final int ITEM_TYPE_0 = 0xff03;
    public final int ITEM_TYPE_1 = 0xff04;
    public final int ITEM_TYPE_2 = 0xff05;
    
    private int imageSize = 0;
    
    public static FindLostFoundRFM newInstance(int type) {
        FindLostFoundRFM myFragment = new FindLostFoundRFM();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        myFragment.setArguments(bundle);
        return myFragment;
    }

    @Override
    public boolean setTitleIsShow() {
        return false;
    }

    @Override
    protected void bindView(View view) {
        super.bindView(view);
        initBus();
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fm_rv_find_lost_found;
    }

    @Override
    protected String setHttpPath() {
        return "Explorer/lostList";
    }

    @Override
    protected JSONObject setHttpParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("type", getArguments().getInt("type"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }

    @Override
    protected List<LostFound> handleHttpData(JSONObject response) {
        List<LostFound> list = new ArrayList<>();
        if (200 == response.optInt("code")) {
            JSONArray array = response.optJSONArray("data");
            for (int i = 0; i < array.length(); i++) {
                JSONObject object = array.optJSONObject(i);
                LostFound model = new LostFound();
                model.setNid(object.optString("id"));
                model.setName(object.optString("title")); //活动标题
                model.setBrowsingNum(object.optString("clicks"));
                //用户信息
                JSONObject user=object.optJSONObject("member");
                model.setUserHead(user.optString("head_img")); //活动发布者头像
                model.setUserName(user.optString("nickname")); //协会名称
                model.setUserTime(object.optString("ctime"));
                model.setCollection_count(object.optString("collection_count"));
                model.setThum(object.optString("thum"));
                JSONArray arrayList = object.optJSONArray("imgs");
                imageSize = arrayList.length();
                model.setImagesize(String.valueOf(imageSize));
                if(arrayList != null){
                    for(int j=0;j<arrayList.length();j++){
                        String img=arrayList.optString(j);
                        switch (j){
                            case 0: model.setIcon1(img);break;
                            case 1: model.setIcon2(img);break;
                            case 2: model.setIcon3(img);break;
                        }
                    }
                }
                switch (imageSize){
                    case 0:
                        model.setType("0");
                        break;
                    case 1:
                        model.setType("1");
                        break;
                    case 2:
                        model.setType("2");
                        break;
                    case 3:
                        model.setType("3");
                        break;    
                }
                list.add(model);
            }
        }
        return list;
    }

    @Override
    protected int setItemViewType(int position) {
        String type = getAdapter().getDatas().get(position).getType();
        if (type.equals("0")){
            return ITEM_TYPE_0;
        }else if (type.equals("1")) {
            return ITEM_TYPE_2;
        }else if (type.equals("2")) {
            return ITEM_TYPE_2;
        }else if (type.equals("3")) {
            return ITEM_TYPE_1;
        }
        return ITEM_TYPE_1;
    }

    @Override
    protected int setItemLayoutId(int viewType) {
        if (viewType == ITEM_TYPE_0){
            return R.layout.item_rv_find_lost_found0;
        }else if (viewType == ITEM_TYPE_1) {
            return R.layout.item_rv_find_lost_found1;
        }else if (viewType == ITEM_TYPE_2) {
            return R.layout.item_rv_find_lost_found2;
        }
        return R.layout.item_rv_find_lost_found1;
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, int position, LostFound model) {
        ((CircleImageView) holder.getView(R.id.head)).setImageURI(model.getUserHead());
        holder.setText(R.id.name, model.getName());
        holder.setText(R.id.nice, model.getUserName());
        holder.setText(R.id.time, model.getUserTime());
        holder.setText(R.id.pageview, model.getBrowsingNum() + "人浏览");
        String size = model.getImagesize();

        switch (getAdapter().getItemViewType(position)) {
            case ITEM_TYPE_0:
                bindNoPic(holder, position, model, size);
                break;
            case ITEM_TYPE_1:
                bindDynamic(holder, position, model, size);
                break;
            case ITEM_TYPE_2:
                bindActivity(holder, position, model, size);
                break;
        }
    }

    public void bindDynamic(RecyclerViewHolder holder, final int position, final LostFound model, final String size) {
        
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance()
                        .build("/find/FindLostFoundInfoRUI")
                        .withString("id", model.getNid())
                        .withString("commentRequestPath","Comment/getLostComment")
                        .withString("headRequestPath","Explorer/lostDetail")
                        .withString("type","9")
                        .withString("position",position+"")
                        .withString("collection_count",model.getCollection_count())
                        .withString("picsize", size)
                        .navigation();
            }
        });
    }

    public void bindActivity(RecyclerViewHolder holder, final int position, final LostFound model, final String size) {
        ((ExpandImageView) holder.getView(R.id.image1)).setImageURI(model.getIcon1());
        
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance()
                        .build("/find/FindLostFoundInfoRUI")
                        .withString("id", model.getNid())
                        .withString("commentRequestPath","Comment/getLostComment")
                        .withString("headRequestPath","Explorer/lostDetail")
                        .withString("type","9")
                        .withString("position",position+"")
                        .withString("collection_count",model.getCollection_count())
                        .withString("picsize", size)
                        .navigation();
            }
        });
    }

    public void bindNoPic(RecyclerViewHolder holder, final int position, final LostFound model, final String size) {
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance()
                        .build("/find/FindLostFoundInfoRUI")
                        .withString("id", model.getNid())
                        .withString("commentRequestPath","Comment/getLostComment")
                        .withString("headRequestPath","Explorer/lostDetail")
                        .withString("type","9")
                        .withString("position",position+"")
                        .withString("collection_count",model.getCollection_count())
                        .withString("picsize", size)
                        .navigation();
            }
        });
    }

    public void initBus() {
        RxBus.getInstance()
                .toObservable(getViewLifecycleOwner(), MsgEvent.class)
                .filter(new Predicate<MsgEvent>() {
                    @Override
                    public boolean test(MsgEvent msgEvent) throws Exception {
                        return msgEvent.getTag().equals(TAG);
                    }
                })
                .subscribe(new Consumer<MsgEvent>() {
                    @Override
                    public void accept(MsgEvent msgEvent) throws Exception {
                        String msg= (String) msgEvent.getMsg();
                        HashMap<String,Object> map=new Gson().fromJson(msg, HashMap.class);
                        int position=Integer.parseInt(map.get("position").toString());
                        LostFound model=getAdapter().getDatas().get(position);
//                        model.setCollection_count(map.get("collection").toString());
                        model.setBrowsingNum(map.get("views").toString());
                        getAdapter().notifyItemChanged(position);
                    }
                });
    }

}
